package com.sgb.goods.view.fragment.manage;

import androidx.annotation.NonNull;

import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsFragmentManageListOperationBinding;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.fragment.BaseFragment;
import com.sgb.goods.utils.helper.SmartRefreshLayoutHelper;
import com.sgb.goods.viewmodel.GoodsManageListOperationViewModel;
import com.sgb.goods.viewmodel.GoodsManageListViewModel;
import com.sgb.goods.widget.DialogHelper;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

/**
 * 商品管理-商品管理列表-管理操作
 * @author cyj
 */
public class GoodsManageListOperationFragment extends BaseFragment<GoodsFragmentManageListOperationBinding, GoodsManageListOperationViewModel>
        implements SmartRefreshLayoutHelper {

    private String status = "";
    private GoodsManageListViewModel mViewModel2;

    public GoodsManageListOperationFragment(String status) {
        this.status = status;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_fragment_manage_list_operation;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(GoodsManageListOperationViewModel.class, BR.viewModel);
        mViewModel2 = createParentViewModel(GoodsManageListViewModel.class);
    }

    @Override
    public void lazyLoadData() {
        mViewModel.status.set(status);
        mViewModel.getGoodsList(false);
    }

    @Override
    protected void initObserve() {
        mViewModel.goodsList.observe(this, goodsList -> {
            refreshUi(mDataBinding.rlvLayout.refreshLayout, mViewModel.adapter, goodsList);
        });

        // 如果页面切换到商品列表页面了，那么重置所有选中
        mViewModel2.isBatch.observe(this, isBatch -> {
            if(!isBatch) {
                mViewModel.adapter.setSelectAll(false);
            }
        });
    }

    @Override
    protected void initListener() {

        // 商品管理列表更新
        mDataBinding.rlvLayout.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                mViewModel.getGoodsList(true);
            }

            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                mViewModel.getGoodsList(false);
            }
        });

        // 上架、下架
        mDataBinding.tvOperation.setOnClickListener(v -> {
            if(Constants.GOODS_OPT_UP.equals(v.getTag())) {
                // 下架
                checkGoodsProperties(Constants.GOODS_OPT_DOWN);
            } else if(Constants.GOODS_OPT_DOWN.equals(v.getTag())) {
                // 上架
                checkGoodsProperties(Constants.GOODS_OPT_UP);
            }
        });

        // 删除
        mDataBinding.tvDelete.setOnClickListener(v -> {
            checkGoodsProperties(Constants.GOODS_OPT_DEL);
        });

        // 全选
        mDataBinding.llSelectDesc.setOnClickListener(v -> {
            v.setTag(null == v.getTag() || !(Boolean) v.getTag());
            mViewModel.adapter.setSelectAll((Boolean) v.getTag());
        });
    }

    /**
     * 检查商品属性以及弹窗提示
     * @param operation    操作类型
     */
    private void checkGoodsProperties(String operation) {
        if(mViewModel.adapter.selectNum.get() <= 0) {
            MToast.showToast(getActivity(), "请先选择商品!");
            return;
        }
        if(Constants.GOODS_OPT_UP.equals(operation)) {
            String msg = "共选择" + mViewModel.adapter.selectNum.get() + "款商品\n商品即将上架对外出售";
            DialogHelper.showDialog(getActivity(), "温馨提示", msg, "确定",
                    new DialogHelper.DialogFunction() {
                @Override
                public void normalRoundDialog() {
                    // 上架
                    mViewModel.upOrDownOrDeleteGoods(Constants.GOODS_OPT_UP);
                }
            });
        } else if(Constants.GOODS_OPT_DOWN.equals(operation)) {
            String msg = "共选择" + mViewModel.adapter.selectNum.get() + "款商品\n商品下架后，将不在平台出售";
            DialogHelper.showDialog(getActivity(), "温馨提示", msg, "确定",
                    new DialogHelper.DialogFunction() {
                        @Override
                        public void normalRoundDialog() {
                            // 下架
                            mViewModel.upOrDownOrDeleteGoods(Constants.GOODS_OPT_DOWN);
                        }
                    });
        } else if(Constants.GOODS_OPT_DEL.equals(operation)) {
            String msg = "共选择" + mViewModel.adapter.selectNum.get() + "款商品\n删除不可恢复，是否确定删除？";
            DialogHelper.showDialog(getActivity(), "温馨提示", msg, "确定",
                    new DialogHelper.DialogFunction() {
                        @Override
                        public void normalRoundDialog() {
                            // 删除
                            mViewModel.upOrDownOrDeleteGoods(Constants.GOODS_OPT_DEL);
                        }
                    });
        }
    }

}